'''
Created on Jan 30, 2012

@author: Forever Together
'''
import logging, os
from mongolog.handlers import MongoHandler

class Log():
    
    @staticmethod
    def getLogger(name, level=logging.DEBUG):
        return Log.getLogger2(name, level=level)
        
    @staticmethod
    def getLogger2(name, fm=None, path=None, level=logging.DEBUG):
        if fm == None: fm = '%(asctime)s - %(levelname)s - line: %(lineno)s - %(message)s'
        logging.basicConfig(format=fm)
        logger = logging.getLogger(name)
        if path != None:
            if not os.path.isdir(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path), 0777)
            handler = logging.FileHandler(path)
            handler.setFormatter(logging.Formatter(fm))
            logger.addHandler(handler)
        logger.setLevel(level)
        return logger
    
    @staticmethod
    def getMongoLog(host, port=27017, name='manaCrawler'):
        logger = logging.getLogger(name)
        logger.setLevel(logging.WARNING)
        logger.addHandler(MongoHandler.to('mongolog', 'logger', host=host, port=port))
        return logger